package com.yang.strategy.mq;

import com.rabbitmq.client.Channel;
import com.yang.common.constant.RabbitMQConstants;
import com.yang.common.exception.StrategyException;
import com.yang.common.model.StandardSubmit;
import com.yang.strategy.flter.StrategyFilterContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author yangyongsheng
 * @date 2024/9/1 23:14
 * @description
 */
@Component
@Slf4j
public class PreSendListener {

    /**
     * 整个策略模块的校验
     */
    @Autowired
    private StrategyFilterContext filterContext;

//接收接口模块发送的消息
@RabbitListener(queues = RabbitMQConstants.SMS_PRE_SEND)
public void listener(StandardSubmit submit, Message message, Channel channel) throws Exception {
    log.info("【策略模块-接收消息】 接收到接口模块发送的消息 submit = {}",submit);

    //业务处理
    try {
        filterContext.strategy(submit);
        log.info("【策略模块-消费完毕】手动ack");
        //手动ACK
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    } catch (StrategyException e) {
        e.printStackTrace();
        log.error("【策略模块-消费失败】校验未通过,msg = {}",e.getMessage());
        //手动ACK
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
}
